#!/usr/bin/env python
# coding=utf-8
# Created by han on 2016/6/5
import os,sys,json,platform,time
if platform.system() == 'Windows':
    BASE_DIR = '\\'.join(os.path.dirname(__file__).split('/')[:-1])
else:
    BASE_DIR = '/'.join(os.path.dirname(__file__).split('/')[:-1])
sys.path.append(BASE_DIR)

from conf import db_conf
from module import accout

def color_msg(arg1,arg2):
    """
    彩色显示提示
    :param arg1:提示字图的颜色
    :param arg2: 提示字体内容
    :return: 真
    """
    msg = "\033[31;{0:d}m {1} \033[33;0m".format(arg1,arg2)
    print(msg)
    return True

def user_chose(*arg):
    """
    接收用户输入，判断是否合法，并返回用户输入
    :param arg: 传入的菜单列表
    :return: 返回用户输入值
    """
    while True:
        for i in enumerate(arg):
            print(i[0]+1,i[1])
        user_inp = input("请输入您的选择或【0】退出： ")
        if user_inp == "0":
            return "0"
        if not user_inp.isdigit() or int(user_inp) > len(arg):
            msg = ("输入有误请重新输入").center(40, ' ')
            color_msg(31, msg)
            continue
        if user_inp :
            return user_inp


def shop_chose(*arg):
    """
    接收用户输入，判断是否合法，并返回用户输入
    :param arg: 传入的菜单列表
    :return: 返回用户输入值
    """
    while True:
        for i in enumerate(arg):
            print(i[0]+1,",",i[1][0],"\t",i[1][1])
        user_inp = input("请输入您的选择或【0】退出,【c】查看购物车： ")
        if user_inp == "0":
            return "0"
        if user_inp == "c":
            return "c"
        if not user_inp.isdigit() or  int(user_inp) > len(arg):
            msg = "输入有误请重新输入".center(40, ' ')
            color_msg(31, msg)
            continue
        if user_inp and int(user_inp) <= len(arg):
            return user_inp


def save_file(file,**obj):
    """
    将创建的用户信息存入文件中
    :param file:存放的文件名称
    :param obj:用户信息
    :return:成功
    """
    local_file = db_conf.DB_BASE+'/'+file
    with open(local_file,'w',encoding="utf-8") as userw_file:
        json.dump(obj,userw_file)
    return True


def load_file(file):
    """
    读取用户信息
    :param file: 读取的文件用户名称
    :return: 返回读取出的用户信息字典
    """
    local_file = db_conf.DB_BASE+'/'+file
    with open(local_file,'r',encoding="utf-8") as userr_file:
        user_dict = json.load(userr_file)
    return user_dict


def save_shop_file(file,**obj):
    """
    将创建的用户信息存入文件中
    :param file:存放的文件名称
    :param obj:用户信息
    :return:成功
    """
    local_file = db_conf.SHOP_DB_BASE+'/'+file
    with open(local_file,'w',encoding="utf-8") as userw_file:
        json.dump(obj,userw_file)
    return True


def load_shop_file(file):
    """
    读取用户信息
    :param file: 读取的文件用户名称
    :return: 返回读取出的用户信息字典
    """
    local_file = db_conf.SHOP_DB_BASE+'/'+file
    with open(local_file,'r',encoding="utf-8") as userr_file:
        user_dict = json.load(userr_file)
    return user_dict



def next_month(day):
    """
    计算下个月的10号日期
    :param day:
    :return:
    """
    local_time = time.localtime()
    mon = local_time.tm_mon
    year = local_time.tm_year
    if mon != 12:
        next_mon = mon+1
        years = year
    else:
        next_mon = 0+1
        years = year+1
    ret = "{year}-{mon}-{day}".format(year=years,mon=next_mon,day=day)
    return ret


def user_format(**user_info):
    """
    格式化用户信息
    :param user_info:接受用户信息的字典
    :return: 返回真
    """
    int_raw=user_info["money"]["interest_raw"] - user_info["money"]["return"]              #逾期未还金额需要将总欠款 - 已经还了的但是没有全还的金额
    notes = "信用卡提示：本信用卡每月22日为记账日，10日为还款日。"
    msg = "\n用户名：{user}\n可用信用额度:{money[avaliable]:>6}元，可用现金{money[money]:>5}元\n未出账单{money[unsettled][1]:>5}元，已出账单{money[out][1]:>5}元" \
          "\n逾期未还{int_raw:>5}元，产生利息{money[interest]:>5}元\n{msg:-^40s}\n".format(int_raw=int_raw,msg=notes,**user_info)
    print(msg)
    return True


def daysdiff(d1, d2):
    """
    计算出两日期之间的差
    :param d1: 较早天数的日期 : 格式为Y-m-d
    :param d2: 较晚天数的日期 : 格式为Y-m-d
    :return: 返回出相差的天数
    """
    old_strp_time = time.strptime(d1, "%Y-%m-%d")   #将时间字符串转换成struct_time
    new_strp_time = time.strptime(d2, "%Y-%m-%d")
    old_stamp = time.mktime(old_strp_time)          #将struct_time 转换成时间戳
    new_stamp = time.mktime(new_strp_time)
    daysec = 24 * 60 * 60                           #计算一天的秒数
    return int((new_stamp - old_stamp) / daysec)                #将两天之间相差的天数返回   两天相差的时间戳 /一天的秒数


def ensure_save(**user_dict):
    user_chose = input("是否确定，【y】确认，其他键返回： ")
    if user_chose == "y":
        accout.ret_info(**user_dict)
        msg = "还款成功，请核对账户".center(40, " ")
        color_msg(32, msg)
        return True
    else:
        msg = "返回".center(40, "-")
        color_msg(33, msg)
        return False